<?
function db_query ( $sql, $echo = false )	{
	global $DATABASE_LINK;
	global $QUERY;
	global $DATABASE;
	global $LANG;
	
	if( !$DATABASE_LINK && !db_connect( $DATABASE ) )
		finalError( sprintf( $LANG['E']['DB_CONNECTION'], str_replace( array( '\'\'', '@' ), '', str_replace( array_values( $DATABASE ), '', db_error( ) ) ), '' ) );
	$result = mysql_query( $sql, $DATABASE_LINK );
	if( error_reporting( ) & E_NOTICE )	{
		if ( mysql_errno( $DATABASE_LINK ) )	{
			echo "\n<br /><b>MySQL ERROR : " . mysql_error( $DATABASE_LINK ) . "</b><br />";
			$echo = true;
		}
		if ( $echo )
			echo "\n<span class=\"atext text\"><br />\n<br /> " . $sql . "</span>";
		$QUERY[] = trim( $sql );
	}
	return ( $result ? $result : ( mysql_errno( $DATABASE_LINK ) == 0 ) );
}

function db_select( $database )	{
	global $DATABASE_LINK;
	if( !$DATABASE_LINK )
		return;
	mysql_select_db( $database, $DATABASE_LINK );
	return ( mysql_errno( $DATABASE_LINK ) == 0 );
}

function db_numrows( $result )	{
	return mysql_num_rows( $result );
}

function db_fetch_array( $result )	{
	return mysql_fetch_assoc( $result );
}

function db_affected_rows ( )	{
	global $DATABASE_LINK;
	if( !$DATABASE_LINK )
		return;
	return mysql_affected_rows( $DATABASE_LINK );
}

function db_connect( $config )	{
	global $DATABASE_LINK;
	global $QUERY;
	
	if( $DATABASE_LINK )
		mysql_close ( $DATABASE_LINK );
	$DATABASE_LINK = null;
	
	$server = ( $config['hostname'] ? ':' . $config['hostname'] : '' ) . ( $config['port'] ? ':' . $config['port'] : '' )  . ( $config['socket'] ? ':' . $config['socket'] : '' );
	$DATABASE_LINK = mysql_connect( $config['hostname'] . ( $config['port'] ? ':' . $config['port'] : '' ) . ( $config['socket'] ? ':' . $config['socket'] : '' ), $config['username'], $config['password'] );
	
	$QUERY[] = 'Connected to ' . $config['hostname'];
	
	if( mysql_errno( ) == 0 && $config['database'] )
		db_select( $config['database'] );
	return ( mysql_errno( ) == 0 );
}

function db_id_insert( )	{
	global $DATABASE_LINK;
	if( !$DATABASE_LINK )
		return;
	return mysql_insert_id( $DATABASE_LINK );
}

function db_error ( )	{
	global $DATABASE_LINK;
	return mysql_error( $DATABASE_LINK );
}

?>